<?php

namespace app\home\controller\Subject;

use app\common\controller\Home;

class Order extends Home
{
    // 课程订单模型
    protected $OrderModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->OrderModel = model('Subject.Order');
    }

    // 首页
    public function index()
    {
        // 如果查询订单时同时有课程，用户的数据
        /* 
            Order [
                'id' => 1,
                'code' => 111,
                ['subject'] => [
                    'title' => php
                ],
                ['business] => [
                    'nickname'
                ]
            ]
        */
        $OrderList = $this->OrderModel->with(['subject'])->where(['busid' => $this->LoginAuth['id']])->select();

        // 消费记录
        $RecordList = model('Business.Record')->where(['busid' => $this->LoginAuth['id']])->select();

        $this->assign([
            'OrderList' => $OrderList,
            'RecordList' => $RecordList
        ]);

        return $this->fetch();
    }

    public function comment()
    {
        $orderid = $this->request->param('orderid',0,'trim');

        $order = $this->OrderModel->with(['subject'])->find($orderid);

        if(!$order)
        {
            $this->error('订单不存在');
        }

        if($this->request->isPost())
        {
            $comment = $this->request->param('comment','','trim');

            if(!$comment)
            {
                $this->error('请输入评价');
            }

            $result = $this->OrderModel->isUpdate(true)->save(['id' => $orderid,'comment' => $comment]);

            if($result === FALSE)
            {
                $this->error('评价失败');
            }else{
                $this->success('评价成功',url('/home/subject/order/index'));
            }
        }

        $this->assign([
            'order' => $order
        ]);

        return $this->fetch();
    }
}
